from rest_framework.generics import ListAPIView
from rest_framework.permissions import *
from apps.meiduo_admin.utils import UserDefinePagination
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import *
from apps.meiduo_admin.serializers.sku import *


class SKUViewSet(ModelViewSet):
    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        return SKU.objects.all()

    serializer_class = SKUSerializer
    pagination_class = UserDefinePagination


class ThreeCategoryListView(ListAPIView):
    # permission_classes = [IsAuthenticated]
    queryset = GoodsCategory.objects.filter(subs=None)
    # queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = GoodsCategorySerializer


class SPUSimpleView(ListAPIView):
    # permission_classes = [IsAuthenticated]
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer


class SPUSpecsView(ListAPIView):
    def get_queryset(self):
        return SPUSpecification.objects.filter(spu_id=self.kwargs["pk"])

    serializer_class = SPUSpecsViewSerializer

